package com.hschinese.pronunciation.iseutil;

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;

import com.hschinese.pronunciation.utils.Logger;
import com.hschinese.pronunciation.utils.UIUtils;
import com.iflytek.cloud.EvaluatorListener;
import com.iflytek.cloud.EvaluatorResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechEvaluator;

/**
 * Created by wo on 2015/11/17.
 */
public class IseTest {
    private Context mContext;
    private SpeechEvaluator mSpeechEvaluator = null;
    private EvaluatorListener mEvaluatorListener = null;
    private String mEvaText = "你好";

    public IseTest(Context ctx) {
        mContext = ctx;
        initData();
    }

    private void initData() {
        mSpeechEvaluator = SpeechEvaluator.createEvaluator(mContext, null);
        // 设置评测语种
        mSpeechEvaluator.setParameter(SpeechConstant.LANGUAGE,"zh_cn");
        // 设置评测题型
        mSpeechEvaluator.setParameter(SpeechConstant.ISE_CATEGORY,"read_word");
        // 设置试题编码类型
        mSpeechEvaluator.setParameter(SpeechConstant.TEXT_ENCODING,"utf-8");
        // 设置前、后端点超时
        mSpeechEvaluator.setParameter(SpeechConstant.VAD_BOS, "5000");
        mSpeechEvaluator.setParameter(SpeechConstant.VAD_EOS, "1800");
        // 设置录音超时，设置成-1 则无超时限制
        mSpeechEvaluator.setParameter(SpeechConstant.KEY_SPEECH_TIMEOUT,"-1");
        // 设置结果等级，不同等级对应不同的详细程度
        mSpeechEvaluator.setParameter(SpeechConstant.RESULT_LEVEL,"complete");
        mSpeechEvaluator.setParameter(SpeechConstant.AUDIO_FORMAT, "wav");
        mSpeechEvaluator.setParameter(SpeechConstant.ISE_AUDIO_PATH, Environment.getExternalStorageDirectory().getAbsolutePath() + "/ise.wav");

        mEvaluatorListener = new EvaluatorListener() {
            // 结果回调，评测过程中可能会多次调用该方法，isLast为true则为最后结果
            public void onResult(EvaluatorResult result, boolean isLast) {
                Logger.e("xionghy-onResult: " + result.getResultString() + " -- isLast: " + isLast);
                if (isLast) {
                    StringBuilder builder = new StringBuilder();
                    builder.append(result.getResultString());
                    UIUtils.showToast(mContext, "评测结束", Toast.LENGTH_SHORT);
                }
            }
            // 出错回调
            public void onError(SpeechError error) {
                Logger.e("xionghy-SpeechError: " + error.getErrorCode());
            }
            // 开始说话回调
            public void onBeginOfSpeech() {
                Logger.e("xionghy-onBeginOfSpeech ...... ");
            }
            // 说话结束回调
            public void onEndOfSpeech() {
                Logger.e("xionghy-onEndOfSpeech ...... ");
            }
            //volume音量值0~30，data音频数据
            public void onVolumeChanged(int volume, byte[] data){
                UIUtils.showToast(mContext, "当前音量：" + volume, Toast.LENGTH_SHORT);
                Logger.e("返回音频数据："+data.length);
            }
            // 扩展接口，暂时没有回调
            public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {}
        };
    }

    public void startEvaluator() {
        mSpeechEvaluator.startEvaluating(mEvaText, null, mEvaluatorListener);
    }

    public void stopEvaluator() {
        mSpeechEvaluator.stopEvaluating();
    }

    public void setChinese(String s) {
        mEvaText = s;
    }
}
